<?php
class WelcomeController extends ApiController
{
    public $login_require = array();
    public $self_error_list = array();
    public function actionAd()
    {
        
        $this->status = $this->error_list['success'];
        $page = Yii::app()->request->getParam('page');
        $page_size = Yii::app()->request->getParam('page_size'); 
        $app_type = Yii::app()->request->getParam('app_id',1);
        $ad_type = 2;
        if ($app_type == 1) {
            $ad_type = 1;
        } 
        $temp = array();
 
        $data = Adv::model()->getlist(1, 999, $ad_type);  
        $result = array();
        $result['ad'] = array();
        $result['version'] =  array();
            $title = Config::model()->get('check_index_text');
            if (!empty($title)) {
                $title = '-'.$title;
            }
        //$result['version']['check_index_text'] = Yii::app()->name.$title;
        $pattern = '/[^\x00-\x80]/';
        if (!empty($data['data'])) {
            foreach ($data['data'] as $key => $value) {
                $temp=array();
                $temp['id'] = $value['id'];
                $temp['img'] = Yii::app()->request->hostInfo.'/images/'.$value['img'].'.png';
                $temp['url'] = $value['url'];
                $result['ad'][] = $temp;
            }
        }
        //版本 
        $platform = Yii::app()->request->getParam('platform');
        $version = Yii::app()->request->getParam('version'); 
        $log = array();
        $log['platform'] = $platform;
        $log['version'] = $version;
        $log['app_type'] = $app_type;
        file_put_contents(Yii::app()->getRuntimePath() . '/welcome_' . date("Y_W") . '.txt', sprintf("%s %s %s", date("Y-m-d H:i:s"), json_encode($log), PHP_EOL), FILE_APPEND);
        if (empty($version) || empty($platform) || !in_array($platform, array('android', 'ios'))) {
             
        } else {
            $platform_int = 0;
            switch ($platform) {
                case 'android':
                    $platform_int = Package::PLATFORM_ANDROID;
                    break;

                case 'ios':
                    $platform_int = Package::PLATFORM_IOS;
                    break;
            }
            $lastPackage = Package::model()->lastPackage($platform_int,$app_type);
            if (!empty($lastPackage)) { 
                if (version_compare($lastPackage['version_name'], $version) == 1) {
                    $result['version']['is_must'] = Config::model()->get('is_must',false);//是否 必须 升级 0  否 1 是
                    if ($result['version']['is_must'] == 1) {
                        $result['version']['is_must'] = true;
                    } else {
                        $result['version']['is_must'] = false;
                    }
                    $result['version']['changelog'] = $lastPackage['changelog'];
                    $result['version']['version'] = $lastPackage['version_name'];
                    $result['version']['allow_mini_version'] = $lastPackage['version_name'];
                    $result['version']['package_url'] = Yii::app()->request->hostInfo . '/download/' . $lastPackage['hash'];
                    if (!empty($lastPackage['url'])) {
                        $result['version']['package_url'] = $lastPackage['url'];
                    }
                    
                    if($app_type == 1)
                    {
                        //车货 
                        if ($platform == 'ios') {
                            $result['version']['package_url'] = 'http://fir.im/zjcgm';
                        }else {
                            $result['version']['package_url'] = 'http://fir.im/zjcgma';
                        }
                    }else {
                        //TMS 
                        if ($platform == 'ios') {
                            $result['version']['package_url'] = 'http://fir.im/zjtm';
                        }else {
                            $result['version']['package_url'] = 'http://fir.im/zjtms';
                        }
                    }
                    
                }
                
            } else {
                
            }
        }
        if (empty($result['version'])) { 
            $result['version'] = (object)array();
        }
        $this->result = $result;
    }
    
}
?>
